﻿myApp.controller("MSSettlementCtrl", ['$scope', '$http', '$routeParams', '$restUrls', '$post', '$rootScope', '$location', function ($scope, $http, $routeParams, $restUrls, $post, $rootScope, $location) {
    var product_id = $routeParams.id;
    $scope.Address = {};
    $scope.Product = {};
    //购买数量
    $scope.num = 1;
    //总价(包括运费)
    $scope.total = 0;
    //单价
    $scope.sale = 0;
    //运费
    $scope.freight = 0;
    $scope.total_not_include_freight = 0;
    $scope.GetAddessList = function () {
        $http({ method: "GET", url: $restUrls["getCustomerInfo"], params: {} }).success(function (result) {
            if (result.success == 1) {
                $scope.Address = result.data.default_addressinfo;
            }
        })
    }
    $scope.GetProductDetail = function (product_id) {
        $http({ method: "GET", url: $restUrls["GetProductDetail"], params: { product_id: product_id } }).success(function (result) {
            if (result.success == 1) {
                $scope.Product = result.data.product;
                $scope.Special = result.data.sepcial;
                $scope.sale=$scope.Special.price;
                //for (var i = 0; i < result.data.product_attribute.length; i++) {
                //    if (result.data.product_attribute[i].attribute_group_id == 7) {
                //        for (var j = 0; j < result.data.product_attribute[i].attribute.length; j++) {
                //            if (result.data.product_attribute[i].attribute[j].attribute_id == 12) {
                //                if (result.data.product_attribute[i].attribute[j].text == '免运费') {
                //                    $scope.freight = 0;
                //                } else {
                //                    $scope.freight = parseFloat(result.data.product_attribute[i].attribute[j].text);
                //                }
                //            }
                //        }
                //    }
                //}
                $scope.total_not_include_freight = parseFloat($scope.sale);
                $scope.total = $scope.freight + parseFloat($scope.sale);

            }
        })
    }
    $scope.GetProductDetail(product_id);
    $scope.GetAddessList();
    $scope.Add_Address = function () {
        window.location.href = "#/add_address";
    }

    //减少购买数量
    $scope.SubProductNum = function (num, product) {
        num == 1 ? $scope.num = 1 : $scope.num = num - 1;
        $scope.total_not_include_freight = parseFloat($scope.sale) * $scope.num;
        $scope.total = $scope.total_not_include_freight + $scope.freight;
    }
    //添加购买数量
    $scope.AddProductNum = function (num, product) {
        if (product.quantity == num) {
            $.scojs_message('抱歉库存不足购买量！', $.scojs_message.TYPE_ERROR);
        } else {
            $scope.num = num + 1;
        }
        $scope.total_not_include_freight = parseFloat($scope.sale) * $scope.num;
        $scope.total = $scope.total_not_include_freight + $scope.freight;
    }
    

    //生成订单
    $scope.paying = false;
    $scope.PayOrder = function () {
        if (!$scope.Address.address_id) {
            $.scojs_message('请选择或添加收货地址', $.scojs_message.TYPE_ERROR);
            return;
        }
        if ($scope.paying) {
            return;
        }
        $scope.paying = true;
        $post($restUrls["generateMsOrder"], { product_id: product_id || 0, quantity: $scope.num, freight: $scope.freight, product_special_id: $scope.Special.product_special_id || 0 }).success(function (result) {
            if (result.success == 1) {
                var order_id = result.data;
                //GenerateOrderPay
                $http({ method: "GET", url: $restUrls["generateOrderPay"], params: { order_id: order_id } }).success(function (result) {
                    result.data = angular.fromJson(result.data);
                    if (result.success == 1) {
                        wx.chooseWXPay({
                            timestamp: result.data.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                            nonceStr: result.data.nonceStr, // 支付签名随机串，不长于 32 位
                            package: result.data.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
                            signType: result.data.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                            paySign: result.data.paySign, // 支付签名
                            success: function (res) {
                                // 支付成功后的回调函数
                                window.location.href = "#/orderlist";
                            }
                        });
                    }
                    $scope.paying = false;
                }).error(function (data, status, headers, config) {
                    $scope.paying = false;
                    $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
                });
            }
            else {
                $scope.paying = false;
                $.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
            }
        }).error(function (data, status, headers, config) {
            $scope.paying = false;
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        })
    };
    ShareOnWx("『新品上市』", "零食4.0——川西新派冷吃兔 私厨珍藏配方160g", "http://haichi.cc/www/index.html#/product/50", "http://haichi.cc/www/img/cp1.jpg");
}]);